﻿@inherits BaseComponent

<Tab @ref="tab" ShowExtendButtons ShowClose>
    @foreach (var item in Items)
    {
        var itemName = Context.Language.GetString(item);
        <TabItem Text="@itemName" Closable="@item.Closable">
            <DynamicComponent @ref="dcs[item.Id]" Type="item.ComType" Parameters="item.ComParameters" />
        </TabItem>
    }
</Tab>

@code {
    private Tab tab;
    private Dictionary<string, DynamicComponent> dcs = [];

    [Parameter] public List<Known.MenuItem> Items { get; set; }
    [Parameter] public Known.MenuItem Menu { get; set; }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);
        var index = Items.IndexOf(Menu);
        tab.ActiveTab(index);
    }

    public override void StateChanged()
    {
        foreach (var dc in dcs)
        {
            (dc.Value.Instance as BaseComponent)?.StateChanged();
        }
    }

    private void OnClose(string tab)
    {
        var item = Items.FirstOrDefault(t => t.Id == tab);
        Items.Remove(item);
    }
}